package cn.xuanxuanyuhua.crm.service.impl;


import cn.xuanxuanyuhua.crm.domain.Role;
import cn.xuanxuanyuhua.crm.mapper.RoleMapper;
import cn.xuanxuanyuhua.crm.query.QueryObject;
import cn.xuanxuanyuhua.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<?> list = roleMapper.selectAll();
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Role entity, Long[] ids) {
        if (entity.getId() != null) {
            roleMapper.deleteRoleAndPermissionRelationByRoleId(entity.getId());
            roleMapper.updateByPrimaryKey(entity);
        }else{
            roleMapper.insert(entity);
        }
        if (ids != null) {
            for (Long id : ids) {
                roleMapper.insertRoleAndPermissionRelationByRoleIdAndPermissionId(entity.getId(),id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteRoleAndPermissionRelationByRoleId(id);
        roleMapper.deleteEmployeeAndRoleRelationByRoleId(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }
}
